package com.haq.housesystem.ctrl;

import com.github.pagehelper.PageInfo;
import com.haq.housesystem.bean.Admins;
import com.haq.housesystem.bean.Houses;
import com.haq.housesystem.service.HousesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.haq.housesystem.ctrl.MyConfig;
@RestController
@RequestMapping("/houses")
public class HousesCtrl {
    @Autowired
    HousesService housesService;
    @Value("${prop.upload-folder}")
    String filesPath;

    @RequestMapping("/insert")
    public Map insert(Houses house,MultipartFile housePic) throws Exception, IOException {
        Map map=new HashMap();
        //createTime  生成当前的系统时间
        SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = s.format(new Date());
        house.setCreateTime(createTime);
        //houseState  新添加肯定是在正常状态  直接设置默认值0
    house.setHouseState(0);

        if(!housePic.isEmpty()) {
            //文件上传
            //获取源文件名   1.jpg   abc.gif
            String filename = housePic.getOriginalFilename();
            //获取后缀
            String zhui=filename.substring(filename.lastIndexOf("."));
            String newFileName="h_"+System.currentTimeMillis()+zhui;
            String filesPath="F:/house/housesweb/housesfiles";
            File file=new File(filesPath,newFileName);
            housePic.transferTo(file);
            house.setPic(newFileName);
        }
        housesService.insert(house);
        map.put("state", "success");
        System.out.println(map);
        return map;
    }
    @RequestMapping("/update")
    public Map update(Houses house,MultipartFile housePic) throws Exception, IOException {
        Map map=new HashMap();
        //createTime  生成当前的系统时间
        SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = s.format(new Date());
        house.setCreateTime(createTime);
        String filesPath="F:/house/housesweb/housesfiles";
        if(!housePic.isEmpty()) {
            //文件上传
            //获取源文件名   1.jpg   abc.gif
            String filename = housePic.getOriginalFilename();
            //获取后缀
            String zhui=filename.substring(filename.lastIndexOf("."));
            String newFileName="h_"+System.currentTimeMillis()+zhui;

            File file=new File(filesPath,newFileName);
            housePic.transferTo(file);

            house.setPic(newFileName);
        }
        housesService.update(house);
        map.put("state", "success");

        return map;
    }
    @RequestMapping("/delete")
    public Map delete(int houseId) {
        housesService.delete(houseId);
        Map map=new HashMap();
        map.put("state", "success");
        return map;
    }
    @RequestMapping("/deleteByIds")
    public Map deleteByIds(int[] houseIds) {
        housesService.deleteByIds(houseIds);
        Map map=new HashMap();
        map.put("state", "success");
        return map;
    }


    @RequestMapping("/selectAll")
    public Map selectAll() {
        List<Houses> list = housesService.selectAll();
        Map map=new HashMap();
        map.put("state", "success");
        map.put("houseslist", list);
        return map;
    }
    @RequestMapping("/selectById")
    public Map selectById(int houseId) {
        Houses house = housesService.selectById(houseId);
        Map map=new HashMap();
        map.put("state", "success");
        map.put("house", house);
        return map;
    }
    @RequestMapping("/selectByTitleTypeOrientState")
    public Map selectByTitleTypeOrientState(String title,@RequestParam(required=false,defaultValue="-1")int typeId,@RequestParam(required=false,defaultValue="-1")int orientId,@RequestParam(required=false,defaultValue="-1")int houseState) {
        List<Houses> list = housesService.selectByTitleTypeOrientState(title, typeId, orientId, houseState);
        Map map=new HashMap();
        map.put("state", "success");
        map.put("houseslist", list);
        return map;
    }

    @RequestMapping("/selectAllByPageInfo")
    public Map selectAllByPageInfo(@RequestParam(required=false,defaultValue="1")int curPage,@RequestParam(required=false,defaultValue="3")int size) {
        PageInfo<Houses> pageInfo = housesService.selectAllByPageInfo(curPage, size);
        Map map=new HashMap();
        map.put("state", "success");
        map.put("houseslist", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }

    @RequestMapping("/selectAllByTitleTypeOrientStateByPageInfo")
    public Map selectAllByTitleTypeOrientStateByPageInfo(@RequestParam(required=false,defaultValue="1")int curPage,@RequestParam(required=false,defaultValue="3")int size,String title,@RequestParam(required=false,defaultValue="-1")int typeId,@RequestParam(required=false,defaultValue="-1")int orientId,@RequestParam(required=false,defaultValue="-1")int houseState) {
        PageInfo<Houses> pageInfo = housesService.selectAllByTitleTypeOrientStateByPageInfo(curPage, size, title, typeId, orientId, houseState);
        Map map=new HashMap();
        map.put("state", "success");
        map.put("houseslist", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }
}
